package com.masnegocio.pac.dto;

import java.io.ByteArrayInputStream;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.masnegocio.pac.comun.Base64;
import com.masnegocio.pac.comun.CodigoMensajeUtil;


public class DatosDto {
	private static final Logger logger = Logger.getLogger(CodigoMensajeUtil.class);
	
	private String version;
	private String sello;
	private String cadenaOriginal;
	private X509Certificate cert;
	private String fechaCreacion;
	private String rfcEmisor;
	private String folio;
	
	public DatosDto (){
		
	}
	
	public DatosDto (String []stringDatos){
		this.setVersion(stringDatos[1]);
		this.setFechaEmision(stringDatos[2]);
		this.setSello(stringDatos[3]);
		this.setCertificado(Base64.decode(stringDatos[4]));
		this.setFolio(stringDatos[5]);
		this.setRfcEmisor(stringDatos[6]);
	}
	
	public PublicKey getCertificado() {
		return cert.getPublicKey();
	}
	public void setCertificado(byte[] keyPAC) {
		CertificateFactory cf;
		try {
			cf = CertificateFactory.getInstance("X.509");
			cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(keyPAC));
		} catch (CertificateException e) {
			logger.error("CERTIFICADO NO VALIDO");
		}
	}
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getSello() {
		return sello;
	}
	public void setSello(String sello) {
		this.sello = sello;
	}
	public String getCadenaOriginal() {
		return cadenaOriginal;
	}
	public void setCadenaOriginal(String cadenaOriginal) {
		this.cadenaOriginal = cadenaOriginal;
	}
	public byte[] getCadenaOriginalDecode(){
		return cadenaOriginal.getBytes();
	}
	public byte[] getSelloDecode(){
		return Base64.decode(sello);
	}
	public void setFechaEmision(String fechaCreacion) {
		this.fechaCreacion = fechaCreacion;
	}
	public String getFechaEmision() {
		return fechaCreacion;
	}
	public void setRfcEmisor(String rfcEmisor) {
		this.rfcEmisor = rfcEmisor;
	}
	public String getRfcEmisor() {
		return rfcEmisor;
	}
	public boolean validaFecha(){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		Date fechaDate = null;
		try {
			fechaDate = simpleDateFormat.parse(fechaCreacion);
		} catch (ParseException e1) {
			if(logger.isDebugEnabled()){
				logger.debug("FECHA MAL MOLFADA");
			}
			return false;
		}
		try{
			cert.checkValidity(fechaDate);
			return true;
		}catch(Exception e){
			if(logger.isDebugEnabled()){
				logger.debug("FECHA NO VALIDA");
			}
			return false;
		}
	}
	public boolean validaRFC(){
		String subject = cert.getSubjectDN().toString();
		String subjectUpper = subject.toUpperCase();
		return subjectUpper.contains(rfcEmisor.toUpperCase());
	}
	public String getFolio() {
		return folio;
	}
	public void setFolio(String folio) {
		this.folio = folio;
	}
}
